$(function(){
    
    class Userlogin{
        constructor(){
            this.username_value = JSON.parse(sessionStorage.getItem( "usermsg" ))
            if( this.username_value !== null){ 
                this.write()
            }
            this.loginClick()
            this.bindEvent()
        }
        bindEvent(){
            $("#login").on( "click" , a=>{
                this.login()
            })
        }
        login(){
            var option = {
                url : "http://127.0.0.1:8888/users/login",
                headers: {"Content-Type" : "application/x-www-form-urlencoded"},
                data : {
                    username : $("#username")[0].value , 
                    password : $("#password")[0].value
                },
            }
            $.post( option ).then( res => {
                if( res.code ){
                    this.success( res )
                }else{
                    this.fail()
                }
            })
        }
        success(res){
            console.log(res.token,res.user.id);
            
            cookie( "TOKEN" , res.token )
            cookie( "ID" , res.user.id )
            location.href = "http://localhost:3000/dist/index.html"
        }
        fail(){
            $(".right b").css({ display : "inline" , color : "red" })
        }
        write(){
            $("#username")[0].value = this.username_value.username
            $("#password")[0].value = this.username_value.password
        }
        loginClick(){
            if( $("#username")[0].value , $("#password")[0].value ){
                $("#login").css({ opacity : 1 })
            }else{
                $("#login").css({ opacity : 0.6 })
            }
        }
    }

    var Login = new Userlogin()

})